package smedi.ismedi.mobile.bff.controller.personnel;

import com.smedi.ismedi.personnel.api.dto.response.InternalAssignmentResponse;
import com.smedi.ismedi.personnel.api.dto.response.PersonResponse;
import com.smedi.ismedi.personnel.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.LogicException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smedi.ismedi.mobile.bff.controller.BaseController;
import smedi.ismedi.mobile.bff.infrastructure.constant.CustomError;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: xulinjun
 * @Date: 2021-7-2 20:11
 */
@Slf4j
@RestController
@Api(tags = {"移动端人员与组织关系接口"})
@RequestMapping("mobilebff/v1/personAssignment")
@AllArgsConstructor
public class PersonAssignmentController extends BaseController {
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    public final static String skipSqlPermission = "ismedi-skip-sql-permission";

    /**
     * 根据人员id获取企业分配组织信息列表
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据人员id获取企业分配组织信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long"),
    })
    @GetMapping(value = "/getInternalAssignmentHrOrganizations")
    public ResponseData<List<InternalAssignmentResponse>> getInternalAssignmentHrOrganizations(Long personId, HttpServletRequest httpServletRequest) {
        // 添加参数
        httpServletRequest.getSession().setAttribute(skipSqlPermission, true);

        ResponseData<PersonResponse> personResponse = remotePersonCoreFeignApi.getQueryPersonDetailInfo(personId);
        if (personResponse.isSuccess()) {
            List<InternalAssignmentResponse> list = personResponse.getData().getInternalAssignments();

            list.stream().forEach(assignment -> {
                assignment.setAssignmentId(null);
                assignment.setPersonId(null);
            });

            return new ResponseData<List<InternalAssignmentResponse>>().success(list);
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, personResponse.getApplication(), String.valueOf(personResponse.getErrorMsg()));
        }
    }

    /**
     * 根据人员id获取项目分配组织信息列表
     *
     * @param personId
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "根据人员id获取项目分配组织信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "personId", value = "人员id", dataType = "Long"),
    })
    @GetMapping(value = "/getProjectAssignmentHrOrganizations")
    public ResponseData<List<ProjectAssignmentResponse>> getProjectAssignmentHrOrganizations(Long personId, HttpServletRequest httpServletRequest) {
        // 添加参数
        httpServletRequest.getSession().setAttribute(skipSqlPermission, true);

        ResponseData<PersonResponse> personResponse = remotePersonCoreFeignApi.getQueryPersonDetailInfo(personId);
        if (personResponse.isSuccess()) {
            List<ProjectAssignmentResponse> list = personResponse.getData().getProjectAssignments();

            list.stream().forEach(assignment -> {
                assignment.setProjectAssignmentId(null);
                assignment.setProjectId(null);
                assignment.setProjectName(null);
                assignment.setProjectShortName(null);
                assignment.setProjectCode(null);
            });

            return new ResponseData<List<ProjectAssignmentResponse>>().success(list);
        } else {
            throw LogicException.raise(CustomError.CALL_REMOTE_API_ERROR, personResponse.getApplication(), String.valueOf(personResponse.getErrorMsg()));
        }
    }
}
